﻿using System; 
namespace LeastSquareMethod

{

    internal class Program
    {
        static double[] AllGdp = new double[66];
        static void Main(string[] args)
        {
            Console.WriteLine("Input predictive year,please");
            double year = Convert.ToDouble(Console.ReadLine()) - 1959;  //把指数函数数据转化为一元函数数据

            double[] Year = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60 };
            double[] GDP =
            {

47209359005,
50056868957,
50706799902,
59708343488,
59716467625,
70436266146,
70846535055,
72881631326,
76720285969,
79705906247,
92602973434,
99800958648,
113687586299,
138544284708,
144182133387,
149540752829,
153940455341,
163431551779,
174938098826,
178280594413,
191149211575,
195866382432,
205089699858,
230686747153,
259946510957,
272972974764,
300758100107,
309488028132,
312353631207,
347768051311,
360857912565,
383373318083,
426915712715,
444731282435,
564324670008,
734547898224,
863746717507,
961603952954,
1029043097558,
1093997267277,
1211346869600,
1339395718862,
1470550015077,
1660287965663,
1955347004965,
2285965892364,
2752131773358,
3550342737009,
4594307032667,
5101703073088,
6087163874510,
7551500124197,
8532229986993,
9570406235659,
10475682920597,
11061553079871,
11233276536744,
12310409370894,
13894817549380,
14279937500608,
14687673892882,

            };
            double[] LnGDP = new double[62];
            for (int n = 0; n < GDP.Length; n++)
            {
                LnGDP[n] = Math.Log(GDP[n], Math.E);
            }
            double sumx = 0, sumy = 0, anvx = 0, anvy = 0;
            double a = 0, b = 0;

            //求横坐标平均值
            for (int i = 0; i < Year.Length; i++)
            {
                sumx += Year[i];
            }
            anvx = (sumx / Year.Length);
            //求纵坐标平均值
            for (int j = 0; j < GDP.Length; j++)
            {
                sumy += LnGDP[j];
            }
            anvy = (sumy / GDP.Length);

            //求斜率
            //求分子
            double numerator = 0;
            for (int k = 0; k < Year.Length; k++)
            {
                numerator += (Year[k] - anvx) * (LnGDP[k] - anvy);
            }
            //求分母
            double denominator = 0;
            for (int k = 0; k < Year.Length; k++)
            {
                denominator += (Year[k] - anvx) * (Year[k] - anvx);
            }
            //正式求斜率
            b = (numerator / denominator);
            a = anvy - b * anvx;

            double gdp = a + b * year;
            gdp = Math.Pow(Math.E, gdp);
            Console.WriteLine(gdp);

            //求所有年份gdp

            for (int i = 0; i < Year.Length; i++)
            {
                AllGdp[i] = a + b * Year[i];
            }
            //判断精度
            double q = Judge(anvy, LnGDP);
            if (q == 1.0)
                Console.WriteLine("完美拟合");
            else
                Console.WriteLine("糟糕拟合");
            Console.WriteLine(q);
        }


        ///判断预测精确度
        static double Judge(double AnvY, double[] gdp)
        {
            double SST = 0, SSE = 0;
            for (int i = 0; i < gdp.Length; i++)
            {
                SST += (gdp[i] - AnvY) * (gdp[i] - AnvY);
            }
            for (int i = 0; i < gdp.Length; i++)
            {
                SSE += (AllGdp[i] - AnvY) * (AllGdp[i] - AnvY);
            }
            double p = SSE / SST;
            return p;
        }
    }
}






